package org.easy.tool.controller;

import org.easy.tool.common.WebUtil;
import org.easy.tool.common.R;
import org.easy.tool.entity.User;
import org.easy.tool.service.IUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *  User模块
 * @author Valley
 * @since 2023-08-25 14:20:43
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    IUser userService;

    /**
     * 功能：注册用户
     */
    @PostMapping("register")
    public R register(@RequestBody User user) throws Exception {
        String userId = userService.addUser(user);
        String token = WebUtil.login(userId);
        return R.ok(token);
    }

    /**
     * 功能：用户登录
     */
    @PostMapping("login")
    public R login(@RequestBody User user) throws Exception {
        User search = userService.getUser(user);
        if (search == null) return R.fail("登录失败");
        String token = WebUtil.login(search.getId());
        return R.ok(token);
    }

}
